package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.chb.cache.aspect.annotation.Cacheable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Tag(name = "专辑详情管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class itemApiController {

	@Autowired
	private ItemService itemService;

	@Autowired
	private SearchService searchService;

	@Operation(summary = "专辑的上架")
	@PostMapping("/albumOnSale/{albumId}")
	public Result albumOnSale(@PathVariable(value = "albumId") Long albumId) {
		itemService.albumOnSale(albumId);
		return Result.ok();
	}

	@Operation(summary = "专辑的下架")
	@DeleteMapping("/albumOffSale/{albumId}")
	public Result albumOffSale(@PathVariable(value = "albumId") Long albumId) {
		itemService.albumOffSale(albumId);
		return Result.ok();
	}

	@Operation(summary = "批量专辑的上架")
	@PostMapping("/batchAlbumOnSale")
	public Result batchAlbumOnSale() {
		for (int i = 1; i <= 1580; i++) {
			itemService.albumOnSale((long) i);
		}

		return Result.ok();
	}
	@Operation(summary = "批量专辑的下架")
	@DeleteMapping("/batchAlbumOffSale")
	public Result batchAlbumOffSale() {
		itemService.batchAlbumOffSale();
		return Result.ok();
	}

//	Request URL: http://localhost:8500/api/search/albumInfo/channel/1
	@Operation(summary = "频道页数据展示")
	@GetMapping("/channel/{category1Id}")
	public Result channel(@PathVariable(value = "category1Id")Long category1Id){
		//是一个7个Map的集合
		//Map的key:baseCategory --> 三级分类对象
		//Map的key:List --> 该三级分类下热度之比较高的6张专辑[List]
		List<Map<String,Object>> mapList = searchService.channel(category1Id);
		return Result.ok(mapList);
	}

	@PostMapping
	@Operation(summary = "待条件的搜索")
	public Result search(@RequestBody AlbumIndexQuery albumIndexQuery){
		AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
		return Result.ok(albumSearchResponseVo);
	}

//	Request URL: http://localhost:8500/api/search/albumInfo/completeSuggest/y
	@GetMapping("/completeSuggest/{input}")
	@Operation(summary = "智能提示词")
	public Result completeSuggest(@PathVariable(value = "input")String input){
		Set<String> set = searchService.completeSuggest(input);
		return Result.ok(set);
	}

	//Request URL: http://localhost:8500/api/search/albumInfo/214

	@GetMapping("/{albumId}")
	@Operation(summary = "根据专辑Id查询专辑详情")
	@Cacheable(cacheKey = RedisConstant.CACHE_INFO_PREFIX + "#{#args[0]}",
			    lockKey = RedisConstant.ALBUM_LOCK_SUFFIX + "#{#args[0]}",
				bloomKey = "#{#args[0]}"
			  ,enableBloomFilter = true
			  ,enableLock = true)  //缓存key就是由程序员自己在用这个注解的时候来指定的
	public Result getAlbumInfo(@PathVariable(value = "albumId") Long albumId){
		Map<String,Object> result = itemService.getAlbumInfo(albumId);
		return Result.ok(result);
	}


	@PostMapping("/preRankingCache")
	@Operation(summary = "提前将Es中的排行榜数据缓存到Redis中")
	public Result preRankingCache(){
		itemService.preRankingCache();
		return Result.ok();
	}

	//Request URL: http://localhost:8500/api/search/albumInfo/findRankingList/1/playStatNum
	@GetMapping("/findRankingList/{category1Id}/{dimension}")
	@Operation(summary = "查询排行榜")
	public Result findRankingList(@PathVariable(value = "category1Id") Long category1Id ,
								  @PathVariable(value = "dimension") String dimension){

		List<AlbumInfoIndexVo> albumInfoIndexVoList = itemService.findRankingList(category1Id,dimension);
		return Result.ok(albumInfoIndexVoList);
	}























}

